package jfox.platform.aaa.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.aaa.entity.Role;
import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;

@Stateless
@Local
@NamedNativeQueries(
		{
			@NamedNativeQuery(
					name = RoleDAOBean.INSERT_ROLE,
					query = "INSERT INTO T_SYS_ROLE( " +
							"ID,NAME,CREATOR,CREATE_TIME,LAST_MODIFIER,LAST_MODIFIED," +
							"LOCAL_NAME,VERSION,REFS,PRIORITY,VSTATUS,DESCRIPTION,NODES) VALUES(" +
							"$ROLE.getId(),$ROLE.getName(),$ROLE.getCreator(),$ROLE.getCreateTime(),$ROLE.getLastModifier(),$ROLE.getLastModified()," +
							"$ROLE.getLocalName(),$ROLE.getVersion(),$ROLE.getRefs(),$ROLE.getPriority(),$ROLE.getVstatus(),$ROLE.getDescription(),$ROLE.getNodes())",
					resultClass = Role.class,
					hints = {
						@QueryHint(name = "cache.partition", value = "role")
					}					
					
			),
			@NamedNativeQuery(
					name = RoleDAOBean.UPDATE_ROLE,
					query = "UPDATE T_SYS_ROLE SET " +
							"NAME = $ROLE.getName(), " +
							"LOCAL_NAME = $ROLE.getLocalName(), " +
							"VERSION = $ROLE.getVersion(), " +
							"PRIORITY = $ROLE.getPriority(), " +
							"REFS = $ROLE.getRefs(), " +
							"VSTATUS = $ROLE.getVstatus(), " +
							"DESCRIPTION = $ROLE.getDescription(), " +
							"NODES = $ROLE.getNodes() " +
							"WHERE ID = $ROLE.getId()",
					resultClass = Role.class,
					hints = {
						@QueryHint(name = "cache.partition", value = "role")
					}
			),
			@NamedNativeQuery(
					name = RoleDAOBean.DELETE_ROLE,
					query = "UPDATE T_SYS_ROLE SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE ID = $ID",
					resultClass = Role.class,
					hints = {
						@QueryHint(name = "cache.partition", value = "role")
					}
			),
			@NamedNativeQuery(
					name = RoleDAOBean.GET_ROLE_BY_ID,
					query = "SELECT * FROM T_SYS_ROLE WHERE ID = $ID" ,
					resultClass = Role.class,
					hints = {
						@QueryHint(name = "cache.partition", value = "role")
					}
			),
			@NamedNativeQuery(
					name = RoleDAOBean.GET_ALL_ROLES,
					query = "SELECT * FROM T_SYS_ROLE WHERE VSTATUS != " + EntityObject.VSTATUS_REMOVED,
					resultClass = Role.class,
					hints = {
						@QueryHint(name = "cache.partition", value = "role")
					}
			)
			
		}
)

/**
 * 
 * @author <a href="mailto:thaiguo@gmail.com">Taiguo Zhang</a>
 *
 */
public class RoleDAOBean extends SuperDAO implements RoleDAO {
	
	public static final String INSERT_ROLE = "RoleDAOBean.insertRole";
	public static final String UPDATE_ROLE = "RoleDAOBean.updateRole";
	public static final String GET_ROLE_BY_ID = "RoleDAOBean.getRoleById";
	public static final String DELETE_ROLE = "RoleDAOBean.deleteRole";
	public static final String GET_ALL_ROLES = "RoleDAOBean.getAllRoles";

	public int deleteRole(long roleId) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("ID", roleId);
		return executeNamedNativeUpdate(RoleDAOBean.DELETE_ROLE,params);	
	}

	public List<Role> getAllRoles() {		
		return (List<Role>) processNamedNativeQuery(RoleDAOBean.GET_ALL_ROLES,null);		
	}

	public Role getRoleById(long id) {
		return (Role) getEntityObjectByColumn(RoleDAOBean.GET_ROLE_BY_ID, "ID", id);
	}

	public void insertRole(Role role) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("ROLE", role);
		executeNamedNativeUpdate(RoleDAOBean.INSERT_ROLE,params);

	}

	public int updateRole(Role role) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("ROLE", role);
		return executeNamedNativeUpdate(RoleDAOBean.UPDATE_ROLE,params);
	}

}
